import { EventSubscriber } from 'typeorm'; // 改用 typeorm 的装饰器
import { EntitySubscriberInterface, InsertEvent } from 'typeorm';
import { snowFlake } from './utils/snow-flake';
import { BaseEntity } from './common/base.entity';

@EventSubscriber() // 使用 typeorm 的 EventSubscriber 装饰器
export class EverythingSubscriber implements EntitySubscriberInterface {
  listenTo() {
    return BaseEntity;
  }

  beforeInsert(event: InsertEvent<any>) {
    console.log('beforeInsert 触发，当前实体：', event.entity);
    if (!event.entity.id) {
      const newId = snowFlake.nextId();
      console.log('生成新ID：', newId);
      event.entity.id = newId;
    }
  }
}
